package com.sunsharing.wsdlcall.wsdl4j;

import com.sunsharing.common.utils.StringUtils;
import org.apache.axis.wsdl.symbolTable.Parameter;

import javax.xml.namespace.QName;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-7-19
 * Time: 上午11:22
 * To change this template use File | Settings | File Templates.
 */
public class WsdlOperatorElement {

    private String name;
    private Parameter type;
    private String typeStr;

    public String getName() {
        return name;
    }

    public String getTypeStr() {
        return typeStr;
    }

    public void setTypeStr(String typeStr) {
        this.typeStr = typeStr;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Parameter getType() {
        return type;
    }

    public void setType(Parameter type) {
        this.type = type;
    }

    public String getStandardInputStr()
    {
        String type = null;
        if(!StringUtils.isBlank(typeStr))
        {
            type = typeStr;
        }else
        {
            QName type2 = org.apache.axis.wsdl.toJava.Utils.getXSIType(this.type);
            type = type2.getLocalPart();
        }

        if(type.equalsIgnoreCase("arrayofstring"))
        {
            return "  <Input name=\""+name+"\" type=\""+type+"\">\n    <Array>\n      <Item>?</Item>\n    </Array>\n  </Input>\n";
        }else if(type.equalsIgnoreCase("arrayofstring2D"))
        {
            return "  <Input name=\""+name+"\" type=\""+type+"\">\n    <Array>\n   <row>   <Item>?</Item>\n  </row>  </Array>\n  </Input>\n";
        }
        else
        {
            return "  <Input name=\""+name+"\" type=\""+type+"\">?</Input>\n";
        }
    }

    public Class getJavaClass()
    {
        String type = null;
        if(!StringUtils.isBlank(typeStr))
        {
            type = typeStr;
        }else
        {
            QName type2 = org.apache.axis.wsdl.toJava.Utils.getXSIType(this.type);
            type = type2.getLocalPart();
        }

        if(type.equalsIgnoreCase("string"))
        {
            return String.class;
        }
        if(type.equalsIgnoreCase("arrayofstring"))
        {
            return String[].class;
        }
        if(type.equalsIgnoreCase("arrayofint"))
        {
            return Integer[].class;
        }
        if(type.equalsIgnoreCase("arrayoflong"))
        {
            return Long[].class;
        }
        if(type.equalsIgnoreCase("arrayofstring2D"))
        {
            return String[][].class;
        }
        if(type.equalsIgnoreCase("int"))
        {
            return Integer.class;
        }
        if(type.equalsIgnoreCase("long"))
        {
            return Long.class;
        }
        if(type.equalsIgnoreCase("float"))
        {
            return Float.class;
        }
        if(type.equalsIgnoreCase("double"))
        {
            return Double.class;
        }
        if(type.equalsIgnoreCase("boolean"))
        {
            return Boolean.class;
        }
        return String.class;
    }

    public String getOutPutString(Object obj,String requestId,String serviceId)
    {
        if(getJavaClass().equals(String.class))
        {
            String result = null;
            if(obj!=null)
            {
                result = obj.toString();
            }else
            {
                result = (String)obj;
            }
            return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<Result>\n" +
                    "<SenderId>"+requestId+"</SenderId>\n" +
                    "<ServiceId>"+serviceId+"</ServiceId>\n" +
                    "<Status>00</Status>\n" +
                    "<ErrorMsg></ErrorMsg>\n" +
                    "<Output type=\"string\">"+transXMLCDADA(result)+"</Output>\n"+
                    "</Result>\n";
        }
        if(getJavaClass().equals(String[].class))
        {

                String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                        "<Result>\n" +
                        "<SenderId>"+requestId+"</SenderId>\n" +
                        "<ServiceId>"+serviceId+"</ServiceId>\n" +
                        "<Status>00</Status>\n" +
                        "<ErrorMsg></ErrorMsg>\n" +
                        "<Output type=\"ArrayOfString\"><Array>";
                if(obj!=null)
                {
                    for(int i=0;i<((String[]) obj).length;i++)
                    {
                        String item = ((String[]) obj)[i];
                        result+="<Item>"+transXMLCDADA(item)+"</Item>";
                    }
                }
                result+=        "</Array></Output>"+"\n</Result>";
            return result;
        }
        if(getJavaClass().equals(String[][].class))
        {

            String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<Result>\n" +
                    "<SenderId>"+requestId+"</SenderId>\n" +
                    "<ServiceId>"+serviceId+"</ServiceId>\n" +
                    "<Status>00</Status>\n" +
                    "<ErrorMsg></ErrorMsg>\n" +
                    "<Output type=\"ArrayOfString2D\"><Array>\n";
            if(obj!=null)
            {
                for(int i=0;i<((String[][]) obj).length;i++)
                {
                    result+="<Row>";
                    String[] item = ((String[][]) obj)[i];
                    for(int j=0;j<item.length;j++)
                    {
                        result+="<Item>"+transXMLCDADA((item[j]==null?"":item[j]))+"</Item>";
                    }
                    result+="</Row>\n";
                }
            }
            result+=        "</Array>\n</Output>"+"</Result>\n";
            return result;
        }
        if(getJavaClass().equals(Boolean.class))
        {
            return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<Result>\n" +
                    "<SenderId>"+requestId+"</SenderId>\n" +
                    "<ServiceId>"+serviceId+"</ServiceId>\n" +
                    "<Status>00</Status>\n" +
                    "<ErrorMsg></ErrorMsg>\n" +
                    "<Output type=\"boolean\">"+transXMLCDADA(obj+"")+"</Output>\n"+
                    "</Result>\n";
        }

        if(obj==null)
        {
            return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                "<Result>\n" +
                "<SenderId>"+requestId+"</SenderId>\n" +
                "<ServiceId>"+serviceId+"</ServiceId>\n" +
                "<Status>00</Status>\n" +
                "<ErrorMsg></ErrorMsg>\n" +
                "<Output type=\"string\">"+transXMLCDADA(obj+"")+"</Output>\n"+
                "</Result>\n";
        } else
        {
            return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<Result>\n" +
                    "<SenderId>"+requestId+"</SenderId>\n" +
                    "<ServiceId>"+serviceId+"</ServiceId>\n" +
                    "<Status>00</Status>\n" +
                    "<ErrorMsg></ErrorMsg>\n" +
                    "<Output type=\"string\">"+transXMLCDADA(obj.toString())+"</Output>\n"+
                    "</Result>\n";
        }

    }

    public static String transXMLCDADA(String input)
    {
        if(input.indexOf("&")!=-1
         ||input.indexOf("<")!=-1
         ||input.indexOf(">")!=-1
         ||input.indexOf("\"")!=-1
         ||input.indexOf("'")!=-1
                )
        {
             return  "<![CDATA["+input+"]]>";
        }else
        {
            return input;
        }
    }

}
